package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    //分页查询权限信息
    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        //分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        if (queryString != null && queryString.length() > 0) {
            queryString = "%" + queryString + "%";
        }
        List<Permission> list = permissionDao.findAll(queryString);
        PageInfo<Permission> pageInfo = new PageInfo<>(list);
        List<Permission> permissionList = pageInfo.getList();
        long total = pageInfo.getTotal();
        return new PageResult(total, permissionList);
    }

    //添加权限
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    //根据id查询单个权限
    public Permission findById(String id) {
        return permissionDao.findById(Integer.parseInt(id));
    }
    //编辑功能
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    @Override
    public void detele(String id) {
        permissionDao.delete(Integer.parseInt(id));
    }
}
